package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.PicturePreviewActivity;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.DensityUtil;
import com.jianyun.jyzs.utils.LoginCache;


import java.util.ArrayList;
import java.util.List;

/**
 * 工程动态图片(音频,视频)展示
 */
public class EngineerAttachGridViewAdapter extends BaseAdapter {


    private final String erpRootUrl;
    private Context context;
    private List<EngineeringResult.EngineerBean.AttachmentBean> atts;
    private ArrayList<String> imageList;

    public EngineerAttachGridViewAdapter(Context context, List<EngineeringResult.EngineerBean.AttachmentBean> attachment, ArrayList<String> imageList) {
        super();
        this.context = context;
        atts = attachment;
        this.imageList = imageList;
        //Log.i("test","图片集合："+imageList.toString());
        erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl()+"DataService/browseImage.aspx?autoid=";

    }


    @Override
    public int getCount() {
        return atts.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        UserViewHolder mHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_img, null);
            mHolder = new UserViewHolder();
            mHolder.head = (ImageView) convertView.findViewById(R.id.itemImage);
            mHolder.playBtn = (ImageView) convertView.findViewById(R.id.chatting_status_btn);
            mHolder.play = (ImageView) convertView.findViewById(R.id.play);
            convertView.setTag(mHolder);
        } else {
            // 取出holder类
            mHolder = (UserViewHolder) convertView.getTag();
        }

        mHolder.playBtn.setVisibility(View.GONE);
        final EngineeringResult.EngineerBean.AttachmentBean bean = atts.get(position);

        String phonPath = bean.getPhonPath();
        String photoPath = bean.getPhotoPath();
        String videoPath = bean.getVideoPath();
        final int i = attachType(phonPath, photoPath, videoPath);
        if (atts.size() == 1) {
            if (i==0){
                RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 80), DensityUtil.dip2px(context, 80));
                lp0.setMargins(0, DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 2), 0);
                mHolder.head.setLayoutParams(lp0);
                lp0 = new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 40), DensityUtil.dip2px(context, 40));
                lp0.addRule(RelativeLayout.CENTER_IN_PARENT);
                mHolder.playBtn.setLayoutParams(lp0);
            }else {
                RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 160), DensityUtil.dip2px(context, 160));
                lp0.setMargins(0, DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 2), 0);
                mHolder.head.setLayoutParams(lp0);
                lp0 = new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 40), DensityUtil.dip2px(context, 40));
                lp0.addRule(RelativeLayout.CENTER_IN_PARENT);
                mHolder.playBtn.setLayoutParams(lp0);
            }
        } else {
            RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 80), DensityUtil.dip2px(context, 80));
            lp0.setMargins(0, DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 2), 0);
            mHolder.head.setLayoutParams(lp0);
            lp0 = new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 40), DensityUtil.dip2px(context, 40));
            lp0.addRule(RelativeLayout.CENTER_IN_PARENT);
            mHolder.playBtn.setLayoutParams(lp0);
        }
        Glide.with(context).load(R.drawable.rc_plugin_default).into(mHolder.head);
        switch (i) {
            case 0:
                final String voiceUrl = erpRootUrl + bean.getId();
                mHolder.head.setBackgroundResource(0);
                Glide.with(context).load(R.drawable.audio_paly_002).into(mHolder.head);
                mHolder.head.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(voiceUrl), "audio/mp3");
                        context.startActivity(intent);
                    }
                });
                break;
            case 1:
                final String portraitUrl = erpRootUrl + bean.getId();
                //Log.i("test","图片："+portraitUrl);
                Glide.with(context).load(portraitUrl)
                        .into(mHolder.head);
                //点击看大图
                mHolder.head.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, PicturePreviewActivity.class);
                        intent.putStringArrayListExtra(SysConstant.msg_key_list1, imageList);
                        intent.putExtra(SysConstant.msg_key_string1, portraitUrl);
                        context.startActivity(intent);
                    }
                });
                break;
            case 2:
                final String videoUrl = erpRootUrl + bean.getId();
                Log.i("test","视频："+videoUrl);
                //glide可加载视频截图
                Glide.with(context).load(videoUrl).into(mHolder.head);
                mHolder.play.setBackgroundResource(R.drawable.gongchengdongtaic_icon_shipin_default);
                mHolder.head.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(videoUrl), "video/mp4");
                        context.startActivity(intent);
                    }
                });

                break;
        }
        return convertView;
    }

    private int attachType(String phonPath, String photoPath, String videoPath) {
        int type = -1;
        if (!TextUtils.isEmpty(phonPath)) type = 0;
        if (!TextUtils.isEmpty(photoPath)) type = 1;
        if (!TextUtils.isEmpty(videoPath)) type = 2;
        return type;
    }

    @Override
    public Object getItem(int position) {
        return atts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    class UserViewHolder {
        public ImageView head;
        public ImageView playBtn;
        public ImageView play;
    }

}  